//package com.punch.order.service.impl;
//
//import cn.hutool.json.JSONUtil;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.punch.common.dto.order.OrderCancelDTO;
//import com.punch.common.dto.order.RefundDTO;
//import com.punch.common.enums.order.OrderStatusEnum;
//import com.punch.common.enums.order.RefundStatusEnum;
//import com.punch.common.exception.BusinessException;
//import com.punch.common.feign.FeignClassService;
//import com.punch.order.exception.OrderExceptionEnum;
//import com.punch.order.mapper.RefundOrderMapper;
//import com.punch.order.model.Order;
//import com.punch.order.model.RefundOrder;
//import com.punch.order.service.OrderService;
//import com.punch.order.service.PayService;
//import com.punch.order.service.RefundService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.util.Map;
//
///**
// * <p>
// * 服务实现类
// * </p>
// *
// * @author wjj
// * @since 2020-11-23
// */
//@Service
//@Slf4j
//public class RefundServiceImpl extends ServiceImpl<RefundOrderMapper, RefundOrder> implements RefundService {
//
//    @Resource
//    @Lazy
//    private PayService payService;
//
//    @Autowired
//    @Lazy
//    private OrderService orderService;
//
//    @Resource
//    private FeignClassService feignClassService;
//
//
//    /**
//     * 取消订单--进行退款
//     *
//     * @param orderEX        主订单信息
//     * @param orderCancelDTO 退款信息
//     */
//    @Override
//    public void refundOrder(Order orderEX, OrderCancelDTO orderCancelDTO) {
//        RefundOrder refundOrder = new RefundOrder();
//        refundOrder.setMainOrderId(orderEX.getId());
//        refundOrder.setOrderNo(orderEX.getOrderNo());
//        refundOrder.setTransactionId(orderEX.getWxTransactionId());
//        refundOrder.setTotalFee(orderEX.getTotalFee());//订单金额
//        /**
//         *退款三种情况
//         * 1.课程不含教具,直接全部退款付款金额 materialRefundFlag == 0
//         * 2.课程包含教具，又分成2种情况
//         *   2.1 教具必须购买,直接全部退款金额 materialRefundFlag == 0
//         *   2.2 教具非必须购买
//         *     2.2.1 课程退款,直接全部退款 materialRefundFlag == 0
//         *     2.2.2 教具退款,可部分退款教具 materialRefundFlag == 1
//         *
//         */
//        //退款金额不能为0
//        if (orderCancelDTO.getRefundFee() <= 0) {
//            throw new BusinessException(OrderExceptionEnum.REFUND_AMOUNT_CAN_NOT_BE_ZERO);
//        }
//        if (orderCancelDTO.getRefundType() == 0) {
//            //退款金额不能大于订单总金额
//            if (orderCancelDTO.getRefundFee() > orderEX.getTotalFee()) {
//                throw new BusinessException(OrderExceptionEnum.REFUND_AMOUNT_GREATER_THAN_TOTAL_AMOUNT);
//            }
//            //申请退款金额
//            refundOrder.setRefundFee(orderCancelDTO.getRefundFee());
//        }
//        //只退款教具情况
//        if (orderCancelDTO.getRefundType() == 1) {
//            //退款金额不能大于教具订单总额
//            if (orderCancelDTO.getRefundFee() > orderEX.getMaterialOrderFee()) {
//                throw new BusinessException(OrderExceptionEnum.REFUND_AMOUNT_GREATER_THAN_TOTAL_AMOUNT);
//            }
//            refundOrder.setRefundFee(orderEX.getMaterialOrderFee());
//        }
//        refundOrder.setRefundType(orderCancelDTO.getRefundType());//退款类型
//        refundOrder.setRefundStatus(0);
//        refundOrder.setRefundDesc(orderCancelDTO.getReason());
//        save(refundOrder);
//    }
//
//    /**
//     * 退款
//     *
//     * @param orderId 订单id
//     * @param type    0-不同意 1-同意
//     * @return long
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public Long refund(Long orderId, Integer type) {
//        QueryWrapper<RefundOrder> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(RefundOrder::getMainOrderId, orderId).eq(RefundOrder::getRefundStatus, 0);
//        RefundOrder refundOrderEX = getOne(queryWrapper);
//        if (refundOrderEX == null) {
//            throw new BusinessException(OrderExceptionEnum.REFUND_ORDER_NOT_EXIST);
//        }
//        //不同意退款
//        if (type == 0) {
//            refusedRefund(orderId, refundOrderEX.getOrderNo(), refundOrderEX.getId(), refundOrderEX.getRefundType());
//        }
//        //同意退款
//        if (type == 1) {
//            RefundDTO refundDTO = new RefundDTO();
//            refundDTO.setTransactionId(refundOrderEX.getTransactionId());
//            refundDTO.setOutTradeNo(refundOrderEX.getOutRefundNo());
//            refundDTO.setTotalFee(refundOrderEX.getTotalFee());
//            refundDTO.setRefundFee(refundOrderEX.getRefundFee());
//            refundDTO.setRefundDesc(refundOrderEX.getRefundDesc());
//            Map<String, String> refundMap = payService.wxRefund(refundDTO);
//            String returnCode = refundMap.get("return_code");
//            //如果返回成功查看返回业务信息
//            if ("SUCCESS".equals(returnCode) && "SUCCESS".equals(refundMap.get("result_code"))) {
//                //状态改成退款中--微信回调异步
//                agreeRefund(orderId, refundOrderEX.getOrderNo(), refundOrderEX.getId(), refundOrderEX.getRefundType(), refundMap);
//            } else {
//                //退款异常
//                errorRefund(orderId, refundOrderEX.getOrderNo(), refundOrderEX.getId(), refundOrderEX.getRefundType(), refundMap);
//            }
//        }
//        return orderId;
//    }
//
//
//    /**
//     * 拒绝退款
//     *
//     * @param orderId    订单id
//     * @param orderNo    订单编号
//     * @param refundId   退款单id
//     * @param refundType 退款类型
//     */
//    private void refusedRefund(Long orderId, String orderNo, Long refundId, Integer refundType) {
//        RefundOrder refundOrder = new RefundOrder();
//        refundOrder.setId(refundId);
//        refundOrder.setRefundStatus(RefundStatusEnum.REFUND_CLOSE.getCode());
//        updateById(refundOrder);
//        Integer status = OrderStatusEnum.REFUSED_REFUND.getCode();
//        String optRemark = "订单退款拒绝";
//        if (refundType == 1) {
//            status = OrderStatusEnum.MATERIAL_REFUSED_REFUND.getCode();
//            optRemark = "教具订单退款拒绝";
//        }
//        //修改主订单状态
//        updateOrder(orderId, orderNo, refundType, status, optRemark, "");
//    }
//
//    /**
//     * 同意退款
//     *
//     * @param orderId    订单id
//     * @param orderNo    订单编号
//     * @param refundId   退款单id
//     * @param refundType 退款类型
//     * @param refundMap  微信返回信息
//     */
//    private void agreeRefund(Long orderId, String orderNo, Long refundId, Integer refundType, Map<String, String> refundMap) {
//        RefundOrder refundOrder = new RefundOrder();
//        refundOrder.setId(refundId);
//        refundOrder.setRefundStatus(RefundStatusEnum.REFUNDING.getCode());
//        updateById(refundOrder);
//        Integer status = OrderStatusEnum.TO_REFUND.getCode();
//        String optRemark = "订单退款中";
//        String infoRemarks = JSONUtil.toJsonStr(refundMap);
//        //修改主订单状态
//        updateOrder(orderId, orderNo, refundType, status, optRemark, infoRemarks);
//        //减少库存 --远程调用
//        Order orderEX = orderService.getById(orderId);
//        feignClassService.cancelSignUpNumber(orderEX.getCourseClassId());
//        //help gc
//        orderEX = null;
//        refundOrder = null;
//    }
//
//    /**
//     * 退款异常
//     *
//     * @param orderId    订单id
//     * @param orderNo    订单编号
//     * @param refundId   退款单id
//     * @param refundType 退款类型
//     * @param refundMap  微信返回信息
//     */
//    private void errorRefund(Long orderId, String orderNo, Long refundId, Integer refundType, Map<String, String> refundMap) {
//        RefundOrder refundOrder = new RefundOrder();
//        refundOrder.setId(refundId);
//        refundOrder.setRefundStatus(RefundStatusEnum.REFUND_ERROR.getCode());
//        updateById(refundOrder);
//        Integer status = OrderStatusEnum.REFUSED_ERROR.getCode();
//        String optRemark = "订单退款异常";
//        String infoRemarks = JSONUtil.toJsonStr(refundMap);
//        //修改主订单状态
//        updateOrder(orderId, orderNo, refundType, status, optRemark, infoRemarks);
//    }
//
//    private void updateOrder(Long orderId, String orderNo, Integer refundType, Integer status, String optRemark, String infoRemarks) {
//        //修改主订单状态
//        Order order = new Order();
//        order.setId(orderId);
//        order.setOrderNo(orderNo);
//        order.setOrderStatus(status);
//        order.setContainMaterialFlag(refundType);
//        orderService.updateOrderStatus(order, optRemark, infoRemarks);
//    }
//}
